<template>
    <div class="app-container">
        <el-card shadow="never">
            <!-- 条件查询 -->
            <el-form v-show="showSearch" ref="queryForm" :model="queryParams" label-width="120px" @keyup.enter.native="submitSearch" @submit.native.prevent>
                <el-row>
                    <el-col :md="6" :sm="12">
                        <el-form-item class="ele-text-right" label-width="50px">
                            <el-button type="primary" size="mini" @click="submitSearch" icon="el-icon-search" class="ele-btn-icon">搜索 </el-button>
                            <el-button @click="resetQuery" size="mini" icon="el-icon-refresh">重置</el-button>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                    <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="open" v-hasPermi="api.add">添加</el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="api.delete">删除</el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button type="warning" plain icon="el-icon-download" size="mini" v-loading="exportLoading" @click="handleExport" v-hasPermi="api.export">导出</el-button>
                </el-col>
                <right-toolbar :showSearch.sync="showSearch" @queryTable="initIndex" :columns="columns" printId="#quickadmin"></right-toolbar>
            </el-row>

            <!-- 列表 -->
            <qa-table
                :source="tableData"
                :columns="columns"
                :selection="false"
                :loading="loading"
                @sortChange="sortChange"
                @statusChange="statusChange"
                @selectionChange="selectionChange"
                id="quickadmin"
            >
                <template v-slot:action>
                    <el-table-column label="操作" width="90" align="left">
                        <template slot-scope="scope">
                            <el-button class="btn-mar" size="mini" type="text" icon="el-icon-download" @click="handleDownLoad(scope.row)">下载</el-button>
                        </template>
                    </el-table-column>
                </template>
            </qa-table>
            <!-- 分页 -->
            <pagination :total="total" :page.sync="pageInfo.page" :limit.sync="pageInfo.limit" @pagination="initIndex" />
        </el-card>
        <!-- 编辑弹窗 -->
        <el-dialog :title="!disabled ? '编辑' : '详情'" :visible="editOpen"  :before-close="beforeClose" destroy-on-close>
            <EditForm ref="editForm" :findFormData="findFormData"  :disabled="disabled" @submitForm="submitForm" :editFormParams="editFormParams" @editFormCancel="beforeClose" />
        </el-dialog>

    </div>
</template>

<script>
import { curdMixin } from '@/mixins/curdMixin'
import EditForm from './EditFormCom'
import Cookies from "js-cookie";
export default {
    name: 'Curd',
    mixins: [curdMixin],
    components: { EditForm },
    props: {
        queryObj: {
            type: Object,
            default: ()=> {
                return {
                    gift_id: '',
                    roomId: ''
                }
            }
        }
    },
    data() {
        return {
            // table结构
            columns: [
                {
                    visible: true,
                    label: '编号',
                    prop: 'id',
                    width: 60
                },
                {
                    visible: true,
                    label: '文件格式',
                    prop: 'file_format',
                },
                {
                    visible: true,
                    label: '文件大小（KB）',
                    prop: 'file_size',
                    formatter: (prop, row)=> {
                        return parseInt(row.file_size / 1000)
                    }
                },
                {
                    visible: true,
                    label: '录制视频',
                    prop: 'video_url',
                    component: "QuickAdminVideo"
                },
                {
                    visible: true,
                    label: '创建时间',
                    prop: 'create_time',
                },
                // {
                //     visible: true,
                //     label: '更新时间',
                //     prop: 'update_time',
                // },
            ],
            // 接口地址
            api: {
                index: `/admin/live.room_transcribe/index`,
                find: `/admin/live.room_transcribe/find`,
                add: `/admin/live.room_transcribe/add`,
                edit: `/admin/live.room_transcribe/edit`,
                delete: `/admin/live.room_transcribe/delete`,
                export: `/admin/live.room_transcribe/export`,
                status: `/admin/live.room_transcribe/status`
            },
            editFormParams: {},
            pageInfo: {
                sort: 'id',
            },
            primaryKey: 'id',
            // 搜索表单是否展开
            searchExpand: false,
            //是否展示搜索
            showSearch: false,
            // 查询参数
            queryParams: {
                room_id: { value:undefined, op: '='},
            },
            tableIsJoinLoad: false
        }
    },
    created () {
        if (this.queryObj.room_id) {
            this.queryParams.room_id.value = this.queryObj.room_id
        }
        this.initIndex()
    },
    methods: {
        handleDownLoad(row) {
            var url //下载地址
            var xhr = new XMLHttpRequest();
            xhr.open('get', row.video_url.replace("http://", "//"), true); // 也可以使用POST方式，根据接口
            xhr.responseType = "blob"; // 返回类型blob
            xhr.onload = function () {
                if (this.status === 200) {
                    var blob = this.response;
                    var reader = new FileReader();
                    reader.readAsDataURL(blob); // 转换为base64，可以直接放入a表情href
                    reader.onload = function (e) {
                        var a = document.createElement('a');
                        a.download = `${row.file_id}.${row.file_format}`; //下载文件名
                        a.href = e.target.result;
                        a.click();
                        window.URL.revokeObjectURL(e.target.result)
                    };
                }
            };
            xhr.send();
        }
    }
}
</script>
<style lang="scss">
.btn-mar {
    margin: 5px !important;
}
</style>
